﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Application.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Application.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;

    public class ProprietorController : EntityController<Proprietor, ProprietorSearch, IProprietorViewModel, IProprietorAppService>, IProprietorController
    {
        private IConsignAppService ConsignAppService { get; set; }
        private IAttachmentAppService AttachmentAppService { get; set; }

        public ProprietorController()
        {
            this.Model = new ProprietorViewModel();
        }

        public override string ControllerName
        {
            get { return ControllerConst.PROPRIETOR; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        [ActionName("detail")]
        public ActionResult GetProprietor(int id)
        {
            IProprietorViewModel proprietorViewModel = ModelFactory.CreateModel<IProprietorViewModel>();
            Proprietor proprietor = this.AppService.GetById(id);
            proprietorViewModel.Item = proprietor;
            proprietorViewModel.Avatar = this.AttachmentAppService.GetProprietorAvatar(proprietor);

            return View(proprietorViewModel);
        }

        [ActionName("popularproprietor")]
        [ChildActionOnly]
        //[OutputCache(Duration = 3600)]
        public ActionResult GetPopularProprietor()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("BidCount", true);

            ConsignSearch consignSearch = ConsignSearchFactory.CreateConsignSearch(ConsignIdentifier.ConsignStatus.IsBidding, orderBy, 1, Project.Settings.PopularProprietorAmount);
            IList<Consign> consigns = this.ConsignAppService.GetConsignsBySearch(consignSearch);
            IList<Proprietor> proprietors = new List<Proprietor>();
            IProprietorsViewModel proprietorsViewModel = ModelFactory.CreateModel<IProprietorsViewModel>();
            foreach (Consign c in consigns)
            {
                proprietors.Add(c.Proprietor);
            }
            proprietorsViewModel.Proprietors = ProprietorViewModelConverter.Convert(proprietors);

            foreach (IProprietorViewModel p in proprietorsViewModel.Proprietors)
            {
                consignSearch = ConsignSearchFactory.CreateConsignSearch(p.Item.ID);
                p.ConsignTotal = this.ConsignAppService.GetCount(consignSearch);
                ConsignBidderSearch consignBidderSearch = ConsignBidderSearchFactory.CreateConsignBidderSearch(p.Item.ID);
                p.ConsignBidderTotal = this.ConsignAppService.GetConsignBidderCount(consignBidderSearch);
            }
            return PartialView("~/Views/Proprietor/_popularProprietorPartial.cshtml", proprietorsViewModel);
        }
    }
}
